import {noop, isELementNode} from './util'
/**
 * @param{string|element} el 移动的主体，必须是element
 * @param{object} options 配置文件，具体内容看下面
 * @param{object.initX} el初始化水平坐标 default:0
 * @param{object.initY} el初始化竖直坐标 default:0
 * @param{object.differX} el水平坐标变化值 default:0
 * @param{object.differY} el竖直坐标变化值 default:0
 * @param{object.initScale} el初始化缩放值 default:1
 * @param{object.differScale} el缩放值，会直接给transform:scale赋值 default:1
 * @param{object.originX} el transform-origin 水平坐标
 * @param{object.originY} el transform-origin 竖直坐标
 * @param{object.positionX} 给transform:translate的水平赋值 = initX + differX
 * @param{object.positionY} 给transform:translate的竖直赋值 = initY + differY
 */
class TransformDom{
    constructor(el, options = {}){
        if (!isELementNode(el)){
            throw new Error('[class TransformDom] need ElementNode as first param')
        }
        this.el = el
        this.initX = options.initX || 0
        this.initY = options.initY || 0
        this.differX = options.differX || 0
        this.differY = options.differY || 0
        this.initScale = options.initScale || 1
        this.differScale = options.differScale || 1
        this.originX = options.originX || '50%'
        this.originY = options.originY || '50%'
        this.isTransformDomInstance = true

        this.positionX = 0
        this.positionY = 0

    }

    setTransformOrigin(x, y){
        this.originX = x
        this.originY = y
        this.transform()
    }

    transform(){
        this.positionX = this.initX + this.differX
        this.positionY = this.initY + this.differY
        this.el.style.cssText = `transform:translate(${this.positionX}px, ${this.positionY}px) translateZ(0) scale(${this.initScale * this.differScale});transform-origin: ${this.originX} ${this.originY}`
    }

    move(x, y){
        this.differX = x
        this.differY = y
        this.transform()
    }

    moveVertical(y){
        this.move(0, y)
    }

    moveHorizontal(x){
        this.move(x, 0)
    }

    scale(s){
        this.differScale = s
        this.transform()
    }
}

export default TransformDom
